package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.BatchQueryManageDeptListReq;
import com.sg.dto.base.framework.req.QueryManageDeptDetailReq;
import com.sg.dto.base.framework.req.QueryManageDeptListReq;
import com.sg.entity.ManagedDept;
import com.sg.service.base.framework.MManagedDeptService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.ManagedDeptMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MManagedDeptServiceImpl
        extends ServiceImpl<ManagedDeptMapper, ManagedDept>
        implements MManagedDeptService {

    @Autowired
    ManagedDeptMapper managedDeptMapper;

    /**
     * 1-2-14新增管理的部门[1352]
     * gen by moon at 10/16/2022, 11:09:28 AM
     */
    @Trace(operationName = "1-2-14新增管理的部门")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addManageDept(ManagedDept managedDept) {
        managedDept.setManagedDepId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        managedDept.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        managedDept.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        managedDept.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        managedDept.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        managedDept.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        managedDept.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(managedDept)) {
            return managedDept.getManagedDepId();
        } else {
            return "-1";
        }

    }

    /**
     * 1-2-14查询管理的部门列表[1152]
     * gen by moon at 10/16/2022, 11:09:27 AM
     */
    @Trace(operationName = "1-2-14查询管理的部门列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<ManagedDept> queryManageDeptList(QueryManageDeptListReq reqDto) {

        try {
            QueryWrapper<ManagedDept> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(ManagedDept.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(ManagedDept.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(ManagedDept.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<ManagedDept>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-14删除管理的部门[1230]
     * gen by moon at 10/16/2022, 11:09:27 AM
     */
    @Trace(operationName = "1-2-14删除管理的部门")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteManageDept(String managedDepId) {

        return super.removeById(managedDepId);
    }

    /**
     * 1-2-14批量删除管理的部门[1131]
     * gen by moon at 10/16/2022, 11:09:26 AM
     */
    @Trace(operationName = "1-2-14批量删除管理的部门")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteManageDept(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 1-2-14查询管理的部门详情[1087]
     * gen by moon at 4/13/2023, 3:35:04 PM
     */
    @Trace(operationName = "1-2-14查询管理的部门详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ManagedDept queryManageDeptDetail(QueryManageDeptDetailReq reqDto) {

        try {

            QueryWrapper<ManagedDept> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<ManagedDept> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                ManagedDept retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-14批量查询管理的部门列表[1149]
     * gen by moon at 10/16/2022, 11:09:26 AM
     */
    @Trace(operationName = "1-2-14批量查询管理的部门列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<ManagedDept> batchQueryManageDeptList(BatchQueryManageDeptListReq reqDto) {

        try {
            QueryWrapper<ManagedDept> queryWrapper = new QueryWrapper<>();

            if (reqDto.getManagedDeptList() == null || reqDto.getManagedDeptList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("role_member_id", reqDto.getManagedDeptList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(ManagedDept.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(ManagedDept.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(ManagedDept.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<ManagedDept>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
